package com.example.demo.controller;

import com.example.demo.mapper.LogInfoMapper;
import com.example.demo.model.LogInfo;
import com.example.demo.model.UserInfo;
import com.example.demo.service.LoggerService;
import com.example.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userInfo;

    @Autowired
    private LoggerService loggerService;

    /**
     * 编程式事务
     * 定义事务和定义事务属性
     */
    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;
    @Autowired
    private TransactionDefinition transactionDefinition;
    @RequestMapping("/register")
    public String register(String userName, String password){
        //开启事务
        TransactionStatus transaction = dataSourceTransactionManager.getTransaction(transactionDefinition);
        Integer result=userInfo.insert(userName,password);
        log.info("注册成功影响行数为：{}",result);
        //事务提交
//        dataSourceTransactionManager.commit(transaction);
        //回滚事务
        dataSourceTransactionManager.rollback(transaction);
        return "用户注册成功";
    }
}
